<?php
/**
 * communit.as
 * @copyright (C)2008 Jaybill McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author Jaybill McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_controllers
 * @license http://communit.as/docs/license License 
 */
 
class Forum_TopicController extends Forum_Controller_Action_Abstract {

	function init(){	

		parent::init();
	}
	
	function indexAction(){
		$request = new Cts_Request($this->getRequest());
		$forums_table = new Forums();
		$topics_table = new Topics();
		$posts_table = new Posts();
		$users_table = new Users();
		$countries_table = new Countries();
		$topicviews_table = new Topicviews();
		$topic_where = "id = ".$request->id;
		$topic = $topics_table->fetchRow($topic_where);
		if(!is_null($topic)){
			$topic = $topic->toArray();
			$topicviews_table->addView($topic['id']);
			if($this->_auth->hasIdentity()){
				$username = $this->_identity->username;
			}else{
				$username = null;
			}

			$params = array('username' => $username, 'topic' => $topic);
		    $additional = $this->_cts_plugin->doFilter($this->_mca, $params); // FILTER HOOK
		    foreach($additional as $key => $value){
		    	$this->view->$key = $value;
		    }

			$forum_where = $forums_table->getAdapter()->quoteInto('id = ?', $topic['forum_id']);
			$forum = $forums_table->fetchRow($forum_where);
			if(!is_null($forum)){
				$this->view->forum = $forum->toArray();
			}
			$per_page = Cts_Registry::get('forum_posts_per_page');
			$posts_where = 'topic_id = '.$request->id;
			$total = $posts_table->getCountByWhereClause($posts_where);

			if($request->has('page')){				
				if($request->page == "last"){
					$page = ceil($total / $per_page) - 1;
				} else {
					$page = $request->page;
				}
			} else {
				$page = 0;
			}

			$url = "/forum/topic/index/id/".$topic['id'];
			$params = array();
			$this->makePager($page, $per_page, $total, $url, $params);

			$posts = $posts_table->fetchAll($posts_where, 'created_on', $per_page, $per_page * $page);
			if(count($posts) > 0){
				$tmp_posts = array();				
				foreach($posts as $post){
					$tmp_post = $post->toArray();
					$user = $users_table->fetchByUsername($tmp_post['username']); 
					if(!is_null($user)){
						$user = $user->toArray();
			    		$user['sign'] = Cts_Common::calculateAstroSign(strtotime($user['birthday']));
			    		$user['age'] = Cts_Common::calculateYearsOld(strtotime($user['birthday']));			    		
			    		$where = $countries_table->getAdapter()->quoteInto('country_code = ?', $user['country_code']);
			    		$country = $countries_table->fetchRow($where);
			    		if(!is_null($country)){
			    			$user['location'] = $country->country; 
			    		}					
						$tmp_post['user'] = $user;
					} 				
					$tmp_post['post'] = $this->_filter->filter($tmp_post['post']);
					$tmp_posts[] = $tmp_post;
				}
				$params = array('posts' => $tmp_posts);
			    $params = $this->_cts_plugin->doFilter($this->_mca . "_posts", $params); // FILTER HOOK
				$this->view->posts = $params['posts'];
			}
		} else {
			$this->_redirect("/default/auth/missing");
		}

	}	

}
